package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Function;
import com.News;

public class UpdNewsServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UpdNewsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("UTF-8");
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		int id = Integer.parseInt(request.getParameter("id"));
		String author = request.getParameter("author");
		String title = request.getParameter("title");
		String classify = request.getParameter("classify");
		String keywords = request.getParameter("keywords");
		String pubtimeHidden = request.getParameter("pubtimeHidden");
		News nw=new News();
		if(request.getParameter("imageName")!=null)
		{
			String image = request.getParameter("imageName");
			image = URLDecoder.decode(image, "UTF-8");
			nw.setImage(image);
		}
		else
		{
			nw.setImage("");
		}
		if(request.getParameter("content")!=null)
		{
			String content = request.getParameter("content");
			content = URLDecoder.decode(content, "UTF-8");
			nw.setContent(content);
		}
		else
		{
			nw.setContent("");
		}
		nw.setId(id);
		nw.setTitle(title);
		nw.setAuthor(author);
		nw.setKeywords(keywords);
		nw.setType(classify);
		//nw.setContent(content);
		nw.setPubtime(pubtimeHidden);
		Function fn= new Function();
		fn.setNw(nw);
		//out.println(nw.getAuthor());
		//out.println(nw.getContent());
		//out.println(nw.getImage());
		//out.println(nw.getKeywords());
		//out.println(nw.getPubtime());
		//out.println(nw.getType());
		
		int n=fn.updnews(nw);
		if(n==1)//添加成功
		{
			response.sendRedirect("newslist.jsp?message=0");
		}
		else//失败
		{
			
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
